package com.coin900.coin.cmd.coin_project;

import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.coin.CoinProjectCoinVO;
import com.coin900.coin.persist.coin.CoinProjectVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.coin.ICoinProjectService;
import com.google.gson.Gson;
import com.value.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 修改项目
 *
 * @author shui
 * @create 2018-01-15
 **/
@Controller
public class CoinProjectPatchCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        CoinProjectVO coinProjectVO = new Gson().fromJson(source, CoinProjectVO.class);
        Long id = coinProjectVO.getId();
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        List<CoinProjectVO> coinProjectVOS = this.projectService.loadCoinProject(params);
        if (coinProjectVOS.isEmpty()) {
            respSign.setMsg("项目不存在");
            respSign.setRespCode(RespCode.FAIL);
            return respSign;
        }

        /***** 修改状态 START *****/

//        CoinProjectVO dbProjectVO = coinProjectVOS.get(0);
//        // 审核状态，0未通过 1通过 2审核中
//        Integer checkStatus = coinProjectVO.getCheckStatus();
//        // 项目状态，0未发布 1进行中 2即将 3已结束
//        Integer status = coinProjectVO.getStatus();
//        // 发布状态 0未发布 1已发布
//        Integer publishStatus = coinProjectVO.getPublishStatus();
//        if (publishStatus == CoinConstants.COIN_PUBLISH_STATUS_PUBLICHED) {
//            // 项目发布，需要判断是否审核通过
//            if (dbProjectVO.getCheckStatus() != CoinConstants.COIN_CHECK_STATUS_SUCCESS) {
//                respSign.setMsg("项目需审核通过才能发布");
//                respSign.setRespCode(RespCode.FAIL);
//                return respSign;
//            }
//            // 项目发布同时，项目状态改为进行中
//            coinProjectVO.setStatus(CoinConstants.COIN_PROJECT_STATUS_ING);
//        }
//
//        if (publishStatus == CoinConstants.COIN_PUBLISH_STATUS_UNPUBLICHED) {
//            // 项目取消发布，项目状态改成0未发布
//            coinProjectVO.setStatus(CoinConstants.COIN_PROJECT_STATUS_UNPUBLISHED);
//        }
//
//        if (status != null && status != 0) {
//            // 修改项目进度，进度不能置0未发布，检查项目是否发布
//            if (dbProjectVO.getPublishStatus() != 1) {
//                respSign.setMsg("项目还未发布");
//                respSign.setRespCode(RespCode.FAIL);
//                return respSign;
//            }
//        }

        /***** 修改状态 END *****/

        List<CoinProjectCoinVO> projectCoinList = coinProjectVO.getProjectCoinList();
        if (projectCoinList != null) {
            for (CoinProjectCoinVO coinProjectCoinVO : projectCoinList) {
                coinProjectCoinVO.setModifiedInfor(user);
            }
        }
        coinProjectVO.setModifiedInfor(user);
        this.projectService.updateCoinProject(coinProjectVO);
        return respSign;
    }

    @Autowired
    private ICoinProjectService projectService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_PROJECT_PATCH;
    }
}
